/**
 * 
 */
package com.great.meal.search.validator;

import java.util.List;

import org.springframework.util.StringUtils;
import org.springframework.validation.Errors;

import com.great.meal.search.dao.CustomerDao;
import com.great.meal.search.domain.Customer;


/**
 * @author lenovo
 *
 */
public class CustomerValidator {
	
	public void validate(Customer customer, Errors errors, CustomerDao cDao) {
		
		if (StringUtils.hasLength(customer.getAccount())) {
			String account = customer.getAccount();
			List<?> qCustomer = cDao.findByNamedQuery("customer.findByAccount", account);
			Object o = qCustomer.get(0);
			if(qCustomer != null && o != null){
				errors.rejectValue("account", "customer_existed", "customer_existed");
			}
		}
		
		String telephone = customer.getContactInfo();
		if (!StringUtils.hasLength(telephone)) {
			errors.rejectValue("contactInfo", "required", "required");
		}else {
			for (int i = 0; i < telephone.length(); ++i) {
				if ((Character.isDigit(telephone.charAt(i))) == false) {
					errors.rejectValue("contactInfo", "nonNumeric", "non-numeric");
					break;
				}
			}
		}
	}

}
